/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2016 - 2021 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit

import joptsimple.internal.Strings
import net.ccbluex.liquidbounce.event.PacketEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.event.repeatable
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.utils.client.chat
import net.minecraft.network.packet.c2s.play.RequestCommandCompletionsC2SPacket
import net.minecraft.network.packet.s2c.play.CommandSuggestionsS2CPacket
import net.minecraft.text.TranslatableText
import net.minecraft.util.Formatting

/**
 * Plugins module
 *
 * Allows you to see a server's plugins.
 */
object ModulePlugins : Module("Plugins", Category.EXPLOIT) {

    override fun enable() {
        if (mc.player != null) {
            network.sendPacket(RequestCommandCompletionsC2SPacket(0, "/"))
        }
    }

    val repeatable = repeatable {
        wait(20)
        chat(TranslatableText("liquidbounce.module.plugins.messages.timeOut").styled { it.withColor(Formatting.RED) })
        enabled = false
    }

    val packetHandler = handler<PacketEvent> { event ->
        val packet = event.packet
        if (packet is CommandSuggestionsS2CPacket) {
            val plugins = ArrayList<String>()
            val cmdList = packet.suggestions.list

            for (cmd in cmdList) {
                val command = cmd.text.split(":")

                if (command.size > 1) {
                    val pluginName = command[0].replace("/", "")

                    if (!plugins.contains(pluginName)) {
                        plugins.add(pluginName)
                    }
                }
            }
            plugins.sort()

            if (plugins.isNotEmpty()) {
                chat("§aPlugins §7(§8${plugins.size}§7): §c${Strings.join(plugins.toArray(arrayOfNulls(0)), "§7, §c")}")
            } else {
                chat(
                    TranslatableText("liquidbounce.module.plugins.messages.noPluginsFound").styled {
                        it.withColor(
                            Formatting.RED
                        )
                    }
                )
            }
            enabled = false
        }
    }
}
